const tokenKey = 'Authorization';

// 获取 token
export function getToken() {
    return localStorage.getItem(tokenKey);
}

// 设置 token
export function setToken(token) {
    return localStorage.setItem(tokenKey, token);
}

// 删除 token
export function removeToken() {
    return localStorage.removeItem(tokenKey);
}

export function isHaveToken() {
    const value = localStorage.getItem('Authorization')
    if (value) {
        return true
    } else {
        return false
    }
}

export function getCurrentUser() {
    const userJson = localStorage.getItem('user');
    if (userJson) {
        try {
            return JSON.parse(userJson);
        } catch (error) {
            console.error('Error parsing user JSON:', error);
            return null;
        }
    } else {
        return null;
    }
}

export function setCurrentUser(user) {
    try {
        const userJson = JSON.stringify(user);
        localStorage.setItem('user', userJson);
    } catch (error) {
        console.error('Error saving user to localStorage:', error);
    }
}

export function removeCurrentUser() {
    try {
        localStorage.removeItem('user');
    } catch (error) {
        console.error('Error removing user from localStorage:', error);
    }
}
